package com.zhangdy.agent.ctrl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zhangdy.agent.entity.MethodPoint;
import com.zhangdy.agent.entity.MethodPointRule;
import com.zhangdy.agent.enums.RuleEnum;
import com.zhangdy.agent.req.LoadAgentReq;
import com.zhangdy.agent.req.MethodRuleReq;
import com.zhangdy.agent.req.SyncMethonRuleReq;
import com.zhangdy.agent.resp.AgentResponse;
import com.zhangdy.agent.service.MethodPointRuleService;
import com.zhangdy.agent.service.MethodPointService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@CrossOrigin
public class MethodController {

    @Autowired
    MethodPointService methodPointService;

    @Autowired
    MethodPointRuleService methodPointRuleService;


    @RequestMapping(value = "methodPointList")
    public AgentResponse list() {
        return AgentResponse.ok(methodPointService.list());
    }


    @RequestMapping(value = "methodRuleList")
    public AgentResponse methodRuleList(@RequestBody MethodRuleReq ruleReq){

        QueryWrapper<MethodPointRule> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .eq(MethodPointRule::getRemoteServerId, ruleReq.getServerId());

        return AgentResponse.ok(methodPointRuleService.list(queryWrapper));
    }

    @RequestMapping(value = "remoteLoadAgent")
    public AgentResponse loadAgent(@RequestBody LoadAgentReq loadAgentReq){

        methodPointService.loadAgent(loadAgentReq.getId());
        return AgentResponse.ok(true);
    }


    @RequestMapping(value = "addMethodPoint")
    public AgentResponse addServer(@RequestBody MethodPoint methodPoint){
        methodPointService.save(methodPoint);
        return AgentResponse.ok(true);
    }

    @RequestMapping(value = "addMethodRule")
    public AgentResponse addMethodRule(@RequestBody MethodPointRule methodRule){

        RuleEnum rule = RuleEnum.getByValue(methodRule.getRule());
        methodRule.setRuleName(rule.name());

        methodPointRuleService.save(methodRule);
        return AgentResponse.ok(true);
    }

    @RequestMapping(value = "syncMethodRule")
    public AgentResponse syncMethodRule(@RequestBody SyncMethonRuleReq req){
        methodPointRuleService.syncRule(req.getMethodRuleId());
        return AgentResponse.ok(true);
    }

    @RequestMapping(value = "deleteMethodRule")
    public AgentResponse deleteMethodRule(@RequestBody MethodPointRule req){
        methodPointRuleService.removeById(req.getId());
        return AgentResponse.ok(true);
    }


    @RequestMapping(value = "modifyMethodRule")
    public AgentResponse modifyMethodRule(@RequestBody MethodPointRule req){
        methodPointRuleService.updateById(req);
        return AgentResponse.ok(true);
    }


}
